<#assign base=request.contextPath />
<!DOCTYPE html>
<html>
<head>
  <base href="${base}">	
  <meta charset="utf-8">
  <title>增加账号</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${base}/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${base}/layuiadmin/layui/css/formSelects.css" media="all">
  <link rel="stylesheet" href="${base}/css/my.css" media="all">
</head>

<body>
  	<div class="layui-form" lay-filter="LAY-form-list" style="padding: 20px 30px 0 0;">
  		<input type="hidden" name="userId" value="${(user.userId)!}">
  	
    	<div class="layui-form-item <#if user??>flex1<#else>flex2</#if>">
      		<label class="layui-form-label required">账号</label>
      		<div class="layui-input-block">
        		<input type="text" name="username" value="${(user.username)!}" lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">
      		</div>
      	</div>
      	<#if !user??>
      	<div class="layui-form-item flex2">
      		<label class="layui-form-label required">密码</label>
      		<div class="layui-input-block">
        		<input type="text" name="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
      		</div>
    	</div>
      	</#if>
    	
    	<div class="layui-form-item flex2">
      		<label class="layui-form-label required">姓名</label>
      		<div class="layui-input-block">
        		<input type="text" name="fullname" value="${(user.fullname)!}" lay-verify="required" placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
      		</div>
      	</div>
      	<div class="layui-form-item flex2">
      		<label class="layui-form-label required">手机</label>
      		<div class="layui-input-block">
        		<input type="text" name="mobile" value="${(user.mobile)!}" lay-verify="required" placeholder="请输入手机号码" autocomplete="off" class="layui-input">
      		</div>
    	</div>  
    	
    	<!-- 组织机构获取测试 -->
    	<div class="layui-form-item flex2">
      		<label class="layui-form-label required">组织机构</label>
      		<div class="layui-input-block">
        		 <input type="text" name="dept.depId" value="${(user.dept.depId)!}"  id="treebox" lay-filter="treebox" class="layui-input" placeholder="组织机构">
      		</div>
      	</div>
      	<div class="layui-form-item flex2">
      		<label class="layui-form-label">状态</label>
      		<div class="layui-input-block">
				 <select name="status" lay-verify="required">
			       <#if user??>
				       <option value="0" ${((user.status!0)==0)?string('selected','')}>禁用</option>
				       <option value="1" ${((user.status!0)==1)?string('selected','')}>激活</option>
			       <#else>
				       <option value="0" >禁用</option>
				       <option value="1" selected>激活</option>
			       </#if>
			     </select>
      		</div>
    	</div>
    	<#--
    	<div class="layui-form-item">
      		<label class="layui-form-label required">民族</label>
      		<div class="layui-input-inline">
    			<select name="nation" lay-verify="required" lay-search>
    			<option value="">---请选择---</option>
      			<@dicts itemName="民族">
      				<#list items as item>
      					<option value="${item.dicId}">${item.itemValue}</option>
      				</#list>
      			</@dicts>
      			</select>
      		</div>
    	</div>
    	-->
	   	<div class="layui-form-item flex1">
	      <label class="layui-form-label">拥有角色</label>
      		<div class="layui-input-block">
				<select name="roleIds" xm-select="select1" id="roles">
            	</select>
            </div>
	    </div>   
	    <div class="layui-form-item layui-hide">
	      	<input type="button" lay-submit lay-filter="LAY-form-submit" id="LAY-form-submit" value="确认添加">
	      	<input type="button" lay-submit lay-filter="LAY-form-edit" id="LAY-form-edit" value="确认编辑">
	    </div>
  	</div>

  <script src="${base}/layuiadmin/layui/layui.js"></script>
  <script>
  	layui.config({
    	base: '${base}/layuiadmin/' //静态资源所在路径
  	}).extend({
    	index: 'lib/index',
    	jwt: '{/}${base}/js/lib/jwt'
  	}).use(['index', 'form', 'jwt','treeSelect','jquery', 'formSelects'], function(){
		var $ = layui.$,
		form = layui.form,
		jwt = layui.jwt;
		treeSelect= layui.treeSelect;
		formSelects = layui.formSelects;
		
		// 下拉选择树
		var ts = treeSelect.render({
			elem: '#treebox',
			data: '${base}/system/dept/treeAll',
			headers: {
				'Authorization': jwt.getCookie('token')
			},
			search: true,
			click: function(d) {
				$('#treebox').val(d.id);
			}
		});
		// 选中下拉树
		var pid = parent.window.parentId;
		if($('#treebox').val()) {
			ts.checkNode('treebox', $('#treebox').val());
		} else if(pid) {
			ts.checkNode('treebox', pid);
		}
	
		// 获得角色
		jwt.ajax({
			async: false,
			url: "${base}/system/role/combo",
			success: function(json) {
				$('#roles').empty();
				for(var i=0; i<json.length; i++) {
					$('#roles').append('<option id = "role'+json[i].roleId+'" value="'+json[i].roleId+'">' + json[i].roleName + '</option>');
				}
				form.render('select');
			}
		});
	
		//多选角色
		formSelects.render('select1', {
			<#if roleIds??>
			init: ${roleIds},
			</#if>
			skin: "danger",
			height: "auto",
			radio: false,
			direction: "auto"
			//max:3
		});
	    
		//监听提交
		form.on('submit(LAY-form-submit)', function(data) {
			var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
			console.log(data)
			//提交ajax成功后，关闭当前弹层并重载表格
			jwt.ajax({
				url: '${base}/system/user/save',
				data: data.field,
				success: function(json) {
					parent.layui.table.reload('LAY-table'); //重载表格
					parent.layer.close(index); //再执行关闭
				}
			});
		});
	
  	})
  </script>
</body>
</html>